package com.quasar.hpatient.module.home_medicine;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.home_medicine.MedicineBean;
import com.quasar.hpatient.module.home_medicine_edit.MedicineEditActivity;
import com.quasar.hpatient.module.home_medicine_record.MedicineRecordActivity;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 服药单
 * created by kalu on 2018/5/3 14:07
 */
public class MedicineActivity extends BaseActivity<MedicinePresenter> implements MedicineView {

    public static final String DATAS = "LIST";
    public static final String POSITION = "POSITION";
    public static final String PATIENTID = "PATIENTID";
    public static final int REQUEST_CODE = 1001;

    @Override
    public int initView() {
        return R.layout.activity_home_medicine;
    }

    @Override
    public void initDataLocal() {

        final NavigationView menu = findViewById(R.id.home_medicine_menu);
        menu.setOnNavigation2ChangeListener((isBack, isMenu1, isMenu2) -> {
            if (isBack) onBackPressed();
            else if (isMenu1) {
                MedicinePresenter presenter = getPresenter();
                presenter.jumpEdit(MedicineActivity.this, -1);
            } else if (isMenu2) {
                final Intent intent = new Intent(getApplicationContext(), MedicineRecordActivity.class);
                goNext(intent);
            }
        });

        final RecyclerView recycler = findViewById(R.id.activity_home_medicine_list);
        getPresenter().initList(this, recycler);
    }

    @Override
    public void initDataNet() {

        final MedicinePresenter presenter = getPresenter();
        final RecyclerView recycler = findViewById(R.id.activity_home_medicine_list);
        presenter.medicineList(this, recycler);
    }

    @Override
    public void goEdit(Intent intent) {
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode != MedicineActivity.REQUEST_CODE || resultCode != MedicineEditActivity.RESULT_CODE)
            return;

        final int intExtra = data.getIntExtra(MedicineEditActivity.EDIT_TYPE, -1);
        if (intExtra == -1) return;

        final RecyclerView recycler = findViewById(R.id.activity_home_medicine_list);

//        // 添加
//        if (intExtra == MedicineEditActivity.EDIT_ADD) {
//            final MedicineBean.MedicineItemBean model = data.getParcelableExtra(MedicineEditActivity.MODEL);
//            if (null != model) {
//                getPresenter().addMedicine(MedicineActivity.this, recycler, model);
//            }
//        }
//        // 修改
//        else if (intExtra == MedicineEditActivity.EDIT_MODIFY) {
//            final int position = data.getIntExtra(MedicineEditActivity.EDIT_POSITION, -1);
//            final MedicineBean.MedicineItemBean model = data.getParcelableExtra(MedicineEditActivity.MODEL);
//            if (null != model) {
//                getPresenter().modifyMedicine(MedicineActivity.this, recycler, model, position);
//            }
//        }
//        // 删除
//        else if (intExtra == MedicineEditActivity.EDIT_DELETE) {
//            final int position = data.getIntExtra(MedicineEditActivity.EDIT_POSITION, -1);
//            getPresenter().deleteMedicine(MedicineActivity.this, recycler, position);
//        }

        initDataNet();
        refreshList(recycler);
        recycler.scrollToPosition(recycler.getAdapter().getItemCount()-1);
    }
}